
#ifndef IMAGELIST_H
#define IMAGELIST_H

#include <QThread>
#include <QDialog>
#include "ui_imagelist.h"
#include "components/thread.h"
#include "network/httprequest.h"
#include <QString>

namespace Wishmaster
{
	class Connector;
	class Request;

	struct PostIcon
	{
		PostId postId;
		QImage image;
		QString piclink;
		bool local;
		int attachNumber;
	};


	class ImageFetchThread : public QThread
	{
	Q_OBJECT
	public:
		ImageFetchThread(bool saveInDb, const ThreadPtr& thread, QString host, QObject* parent = 0);
		virtual ~ImageFetchThread();

		void setPiclinks(QStringList piclinks);
		void setIconList(QList<PostIcon> icons);
		void setTags(QStringList tags);
		void setPath(const QString& path);

		virtual void run();

	signals:
		void progress(int done, int total);

	protected slots:
		void requestFinished(const HttpRequestPtr& r, const QByteArray& arr);

	private:
		QString m_path;
		QList<PostIcon> m_icons;
		QStringList m_piclinks;
		QStringList m_tags;
		Wishmaster::Connector* m_connector;
		int m_totalPics;
		int m_donePics;
		bool m_saveInDb;
		ThreadPtr m_thread;
		QString m_host;
	};
}

class ImageListItem : public QListWidgetItem
{
public:
	ImageListItem(QListWidget* parent);
	virtual ~ImageListItem();
	void setPiclink(const QString& pic);
	QString piclink();

	static const int ImageListItemType = QListWidgetItem::UserType + 1;

private:
	QString m_piclink;
};

class ImageList : public QDialog
{
	Q_OBJECT
public:
	ImageList(QWidget* parent = 0);
	virtual ~ImageList();

	void setThread(Wishmaster::ThreadPtr thread);

public slots:
	void save();
	void saveAsFile();
	void refreshThreadDone();
	void progress(int done, int total);
	void itemAdd(QListWidgetItem* item);
	void itemRemove(QListWidgetItem* item);
	void addTag();
	void tagChanged(const QString& tag);
	void addFut(QListWidgetItem* item);
	void emptyTagLine();
	void addAll();
	void removeAll();

protected:
	void refreshLists();
	QStringList piclinks(const QString& host);
	QList<Wishmaster::PostIcon> icons();

private:
	Wishmaster::ThreadPtr m_thread;
	Ui::ImageList* m_ui;
	QList<Wishmaster::PostIcon> m_icons;

friend class RefresherThread;
};

#endif

